<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Adv extends MY_Controller {
     var $_templates=array();
     var $_lang = '';
    function __construct() {
        parent::__construct();
        $this->_lang = $this->lang->lang();
		//$this->lang->load('adv');
        $this->load->model('adv_model','adv');

        $this->adv->initialise($this->_lang);
    }
	public function load_adv()
	{
        $data = array();
        $adv_page_code = $this->input->post('adv_page');
        $rs_position = $this->adv->loadAdvPositionByPage($adv_page_code);
        foreach ($rs_position as $position){
            $data[$position->adv_position_code]='';
            $rs_adv = $this->adv->loadAdvByPosition($position->adv_position_id);
            if($rs_adv){
                $width=($position->adv_position_width!='')?'width="'.$position->adv_position_width.'px"':'';
                $height=($position->adv_position_height!='')?'height="'.$position->adv_position_height.'px"':'';
                if(count($rs_adv)>0){
                    $data[$position->adv_position_code].='<ul>';
                    foreach ($rs_adv as $adv){
                        $src = ($adv->adv_link!='')?$adv->adv_link:base_url().'uploads/images/adv/'.$this->_lang.'/'.$adv->adv_file;
                        $url = ($adv->adv_url!='')?$adv->adv_url:'#';
                        if($adv->adv_file_type=='IMG'){
                            $data[$position->adv_position_code].='<li onclick="anw_adv.adv_click(\''.$adv->adv_id.'\');"><a title="'.$adv->adv_title.'" href="'.$url.'" target="_blank">
                                <img '.$width.' '.$height.' src="'.$src.'"/>
                            </a></li>';
                        }else{
                            $data[$position->adv_position_code].='<li>
                                <embed  type="application/x-shockwave-flash" wmode="transparent" '.$width.' '.$height.' src="'.$src.'"/>
                            </li>';
                        }
                    }
                    $data[$position->adv_position_code].='</ul>';
                }
            }
        }
        echo json_encode($data);
	}
    function adv_click(){
        $adv_id =$this->input->post('adv_id');
        $rs_adv = $this->adv->getAdvById($adv_id);

        if($rs_adv){
            $limitseconds= 900;//15 minutes
            $rs_second = $this->adv->secondsClick($adv_id,$this->input->ip_address());
            echo $rs_second->total;
            echo '/'.$rs_second->secondsclick;
            if($rs_second->total==0){
                $nb_click = $rs_adv->nb_click+1;
                if($this->adv->saveAdv($adv_id,array('nb_click'=>$nb_click))){
                    $this->write_log($adv_id,1);
                }
            }else if($rs_second->total>0&&$rs_second->secondsclick>=$limitseconds){
                $nb_click = $rs_adv->nb_click+1;
                if($this->adv->saveAdv($adv_id,array('nb_click'=>$nb_click))){
                    $this->write_log($adv_id,1);
                }
            }
        }else{
            
        }
        return true;
    }
    function adv_view(){
        $adv_page_code = $this->input->post('adv_page');
        $rs_position = $this->adv->loadAdvPositionByPage($adv_page_code);
        foreach ($rs_position as $position){
            $rs_adv = $this->adv->loadAdvByPosition($position->adv_position_id);
            if($rs_adv){
                if(count($rs_adv)>0){
                    foreach ($rs_adv as $adv){
                        $nb_view = $adv->nb_view+1;
                        if($this->adv->saveAdv($adv->adv_id,array('nb_view'=>$nb_view))){
                            $this->write_log($adv->adv_id,2);
                        }
                    }
                }
            }
        }
       return true;
    }
    /*
     * @author: Vo Van Hieu
     * @method: write log click or view adv
     * @param : $adv_id - id banner adv
     *          $log_type - type log 1: click 2: view
     * @date  : 3/12/2012 
     */
    function write_log($adv_id,$log_type)
	{
        $this->load->library('user_agent');
        
		$Url = $this->session->userdata('Url');
		$data = array(
			'adv_id' => $adv_id,
            'adv_log_type' => $log_type,
			'adv_log_url' => $Url,
			'adv_log_info' =>$this->agent->agent_string(),
			'adv_log_ip' => $this->input->ip_address(),
			'dt_log' => time()
		);
		$this->adv->writeAdvLog($data);
	}
}

/* End of file adv.php */
/* Location: ./application/controllers/adv.php */